import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/modules/application/standardization/detail/ctrl.dart';

/// 标准化详情
class StandardizationDetailPage extends StatelessWidget {
  StandardizationDetailPage({super.key});

  final ctrl = Get.put(StandardizationDetailController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: ctrl,
      builder: (_) {
        return Scaffold(
          backgroundColor: AppColors.Pure_gray_bg,
          appBar: CustomAppBar(
            titleText: ctrl.type.str,
          ),
          body: Column(
            children: [
              CustomCard(
                title: ctrl.type.str,
                divider: CustomCard.builtinDividerWithPadding,
                content: _buildBody(),
              ).paddingAll(13.w)
            ],
          ),
        );
      },
    );
  }

  Widget _buildBody() {
    if (ctrl.data.length == 2) {
      return Row(
        children: ctrl.data.keys.map(_buildItem).toList(),
      );
    }
    final children = <Widget>[];
    for (int i = 0; i < ctrl.data.length; i += 2) {
      if (ctrl.data.length - i >= 2) {
        children.add(Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            _buildItem(ctrl.data.keys.elementAt(i)),
            if (i + 1 < ctrl.data.length) _buildItem(ctrl.data.keys.elementAt(i + 1)),
          ],
        ));
      } else {
        children.add(_buildLongItem(ctrl.data.keys.elementAt(i)));
      }
    }
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: children,
    );
  }

  /// 一行两个
  Widget _buildItem(String k) {
    final v = ctrl.data[k] ?? '未知';
    return Container(
      decoration: BoxDecoration(
        color: const Color(0x1A5C6DFF),
        borderRadius: BorderRadius.circular(7.w),
      ),
      margin: EdgeInsets.symmetric(vertical: 7.w),
      padding: EdgeInsets.symmetric(vertical: 7.w, horizontal: 27.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            k,
            style: TextStyle(
              fontSize: 14.w,
              color: const Color(0xFF3141CD),
            ),
          ),
          Text(
            v,
            style: TextStyle(
              fontSize: 14.w,
              color: v == '已做' ? Colors.green : Colors.red,
            ),
          )
        ],
      ),
    );
  }

  /// 一行一个
  Widget _buildLongItem(String k) {
    final v = ctrl.data[k] ?? '未知';
    return Container(
      decoration: BoxDecoration(
        color: const Color(0x1A5C6DFF),
        borderRadius: BorderRadius.circular(7.w),
      ),
      margin: EdgeInsets.symmetric(vertical: 7.w, horizontal: 11.w),
      padding: EdgeInsets.symmetric(vertical: 7.w, horizontal: 27.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            k,
            style: TextStyle(
              fontSize: 14.w,
              color: const Color(0xFF3141CD),
            ),
          ),
          Text(
            v,
            style: TextStyle(
              fontSize: 14.w,
              color: v == '已做' ? Colors.green : Colors.red,
            ),
          )
        ],
      ),
    );
  }
}
